﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Bll;
using Runda.EShop.Common.Entity;

namespace Runda.EShop.SaaS.SysManage
{
    public partial class NoticeAdd : BasePage
    {

        #region 属性

        public Guid NoticeID
        {
            get
            {
                if (ViewState["NoticeID"] == null)
                    return Guid.Empty;
                else
                    return new Guid(ViewState["NoticeID"].ToString());
            }
            set { ViewState["NoticeID"] = value; }
        }

        public bool IsAdd
        {
            get { return Convert.ToBoolean(ViewState["IsAdd"].ToString()); }
            set { ViewState["IsAdd"] = value; }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitForm();

                if (!string.IsNullOrEmpty(Request.QueryString["NoticeID"]))
                {
                    IsAdd = false;
                    NoticeID = new Guid(Request.QueryString["NoticeID"]);
                    SetFormData();

                }
                else
                {
                    IsAdd = true;
                    NoticeID = Guid.NewGuid();
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string Error = "";
            if (!TrySave(ref Error))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('" + Error + "')", true);
                return;
            }

            ClientScript.RegisterStartupScript(this.GetType(), "SaveAndClose", "alert('保存成功.');parent.$.ligerDialog.hide();parent.$('#cphMain_cphMain_btnQuery').click();", true);
        }

        private void InitForm()
        {

        }

        private bool TrySave(ref string Message)
        {
            SystemBll bll = new SystemBll();
            SysNoticeEntity entity = null;
            //try save user info
            if (!IsAdd)
            {
                entity = bll.Get<SysNoticeEntity>(NoticeID);
            }
            else
            {
                entity = new SysNoticeEntity();
                entity.NoticeID = NoticeID;
                entity.CreatedAt = DateTime.Now;
                entity.CreatedBy = base.User.Identity.LoginUser.UserID;
                entity.CreatedName = base.User.Identity.LoginUser.UserName;
            }
            entity.Title = txtTitle.Text;
            entity.Content = txtContent.Text;

            bll.InsertOrUpdate(entity);
            return true;

        }

        private void SetFormData()
        {
            if (NoticeID != Guid.Empty)
            {
                SysNoticeEntity entity = null;
                SystemBll bll = new SystemBll();
                entity = bll.Get<SysNoticeEntity>(NoticeID);

                txtTitle.Text = entity.Title;
                txtContent.Text = entity.Content;

            }
        }
    }
}